package com.seeyon.apps.kk.form.workflowevent;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.collect.Maps;
import com.seeyon.apps.kk.model.MemberYearDay;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;

/**
 *
 * 请假单年假校验
 * 
 * @author Administrator
 *
 */
public class QingJiaDanExtendPlug extends KkBaseExtendPlug {

	private JdbcTemplate hrJdbcTemplate;

	public JdbcTemplate getHrJdbcTemplate() {
		return hrJdbcTemplate;
	}

	public void setHrJdbcTemplate(JdbcTemplate hrJdbcTemplate) {
		this.hrJdbcTemplate = hrJdbcTemplate;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "kkQingJiaDanExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "请假单年假校验事件";
	}

	/**
	 * 返回指定的模版编号
	 * 
	 * @return
	 */
	public String getTemplateCode() {
		return "";
	};

	// 发起前事件
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		Map<String, Object> formdata = data.getBusinessData();
		// FormBean formBean = (FormBean) formdata.get("formBean");

		List xjlxs = (List) formdata.get("field0009");// 休假类型
		List memIds = (List) formdata.get("field0005"); // 姓名ID
		List startList = (List) formdata.get("field0010"); // 开始日期
		List endList = (List) formdata.get("field0011"); // 结束日期
		List days = (List) formdata.get("field0042"); // 天数
		List hours = (List) formdata.get("field0041"); // 小时数
		List years = (List) formdata.get("field0111"); // 年度
		List ygbhs = (List) formdata.get("field0033");// 员工编号类型

		Map<String, MemberYearDay> memYear = Maps.newHashMap();

		for (int i = 0; i < xjlxs.size(); i++) {
			String xjenumid = (String) xjlxs.get(i);
			String memberCode = (String) ygbhs.get(i);

			if (("年假").equals(xjenumid)) {
				String memberid = String.valueOf(memIds.get(i));
				Date start = (Date) startList.get(i);
				Date end = (Date) endList.get(i);
				BigDecimal ryear = (BigDecimal) years.get(i);
				BigDecimal day = (BigDecimal) days.get(i);
				BigDecimal hour = (BigDecimal) hours.get(i);

				if (StringUtils.isNotBlank(String.valueOf(ryear))) {
					String key = memberid + "_" + memberCode + "_" + ryear;
					MemberYearDay my = memYear.get(key);
					if (my == null) {
						my = new MemberYearDay();
					}
					my.setMemberId(memberid);
					my.setMemberCode(memberCode);
					my.setYear(ryear.toString());
					BigDecimal total = day.multiply(BigDecimal.valueOf(8)).add(hour);
					my.setDangqian(total.add(my.getDangqian()));

					memYear.put(key, my);
				}
			}

		}

		for (Iterator<String> iterator = memYear.keySet().iterator(); iterator.hasNext();) {
			String mkey = iterator.next();
			String[] keys = StringUtils.split(mkey, "_");
			Map<String, Object> pm = Maps.newHashMap();
			pm.put("memberId", keys[0]);
			pm.put("memberCode", keys[1]);
			pm.put("year", keys[2]);
			// 获取第三方数据库中的期初和已用数据
			String sql = freemarkFlowManager.fltTemplateIntoString("sql/hr/XJSQ_HR.ftl", pm);
			List<Map<String, Object>> vl = this.hrJdbcTemplate.queryForList(sql);
			if (!vl.isEmpty()) {
				Map<String, Object> vm = vl.get(0);
				MemberYearDay my = memYear.get(mkey);
				Integer qic = MapUtils.getInteger(vm, "ENTL_HRS");
				my.setQichu(BigDecimal.valueOf(qic));
				Integer yiy = MapUtils.getInteger(vm, "TAKE_HRS");
				my.setYiyong(BigDecimal.valueOf(yiy));
				memYear.put(mkey, my);
			}
			// 获取OA数据库中的在途数据
			sql = freemarkFlowManager.fltTemplateIntoString("sql/hr/XJSQ_ZT.ftl", pm);
			vl = this.kkJdbcTemplate.queryForList(sql);
			if (!vl.isEmpty()) {

				for (Iterator<Map<String, Object>> iterator2 = vl.iterator(); iterator2.hasNext();) {
					Map<String, Object> vm = iterator2.next();
					MemberYearDay my = memYear.get(mkey);
					Double xs = MapUtils.getDouble(vm, "field0041");
					Double ts = MapUtils.getDouble(vm, "field0042");
					my.setZaitu(BigDecimal.valueOf(ts * 8 + xs).add(my.getZaitu()));
					memYear.put(mkey, my);

				}

			}

		}

		for (Iterator<String> iterator = memYear.keySet().iterator(); iterator.hasNext();) {
			String mkey = iterator.next();
			MemberYearDay my = memYear.get(mkey);
			System.out.println(my.getQichu());
			System.out.println(my.getYiyong());
			System.out.println(my.getDangqian());
			System.out.println(my.getZaitu());
			if (!my.getIsOk()) {
				WorkflowEventResult er = new WorkflowEventResult();
				if (my.getZaitu().intValue() == 0)
					er.setAlertMessage("员工编号 " + my.getMemberCode() + " 年假余额不足！");
				/*
				 * else er.setAlertMessage("员工编号 " + my.getMemberCode() +
				 * " 年假余额不足！\n" + my.getZaitu() + "小时年假待审批中！");
				 */
				return er;
			}
		}

		return null;
	}

}
